﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AutoHistory.Core;

namespace AutoHistory2010ForExcel
{
    public partial class SettingsForm : Form
    {
        List<LanguageItem> languages = new List<LanguageItem>();

        public SettingsForm()
        {
            InitializeComponent();

            languages.Add(new LanguageItem() { DisplayName = "Deutsch", LCID = 1031 });
            languages.Add(new LanguageItem() { DisplayName = "English", LCID = 1033 });
            languages.Add(new LanguageItem() { DisplayName = "Nederlands", LCID = 1043 });
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            try
            {
                Settings.Instance.MaxHistoryFiles = (int)numericUpDownMaxFiles.Value;

                int oldLang = Settings.Instance.Language;
                Settings.Instance.Language = ((LanguageItem)comboBoxLanguage.SelectedItem).LCID;

                bool oldUse = Settings.Instance.UseGlobalHistoryLocation;
                Settings.Instance.UseGlobalHistoryLocation = checkBoxUseGlobalHistory.Checked;

                bool historyLocationChanged = oldUse != checkBoxUseGlobalHistory.Checked;

                if (historyLocationChanged)
                {
                    DialogResult dr = MessageBox.Show(Resource.SettingsHistoryPathChanged, "AutoHistory", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr != DialogResult.Yes)
                        return;
                }

                if (checkBoxUseGlobalHistory.Checked)
                    Settings.Instance.GlobalHistoryLocation = textBoxGlobalHistoryLocation.Text;
                else
                    Settings.Instance.GlobalHistoryLocation = string.Empty;

                Settings.Instance.Save();

                if (oldLang != Settings.Instance.Language || historyLocationChanged)
                {
                    MessageBox.Show(Resource.SettingsRebootRequiredMessage, "AutoHistory", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error saving settings! Please check the discussion forum on autohistory.codeplex.com." + Environment.NewLine + Environment.NewLine + "Message: " + ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace, "AutoHistory", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            PopulateLanguages();

            numericUpDownMaxFiles.Value = (decimal)Settings.Instance.MaxHistoryFiles;
            comboBoxLanguage.SelectedItem = languages.FirstOrDefault(m => m.LCID == Settings.Instance.Language);
            checkBoxUseGlobalHistory.Checked = Settings.Instance.UseGlobalHistoryLocation;
            textBoxGlobalHistoryLocation.Text = Settings.Instance.GlobalHistoryLocation;

            textBoxGlobalHistoryLocation.Enabled = checkBoxUseGlobalHistory.Checked;
            buttonBrowseGlobalHistory.Enabled = checkBoxUseGlobalHistory.Checked;
        }

        private void PopulateLanguages()
        {
            comboBoxLanguage.Items.Clear();
            foreach (var item in languages)
            {
                comboBoxLanguage.Items.Add(item);
            }
        }

        private void buttonBrowseGlobalHistory_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog fb = new FolderBrowserDialog())
            {
                fb.ShowNewFolderButton = true;
                DialogResult dr = fb.ShowDialog(this);
                if (dr == DialogResult.OK)
                {
                    textBoxGlobalHistoryLocation.Text = fb.SelectedPath;
                }
            }
        }

        private void checkBoxUseGlobalHistory_CheckedChanged(object sender, EventArgs e)
        {
            textBoxGlobalHistoryLocation.Enabled = checkBoxUseGlobalHistory.Checked;
            buttonBrowseGlobalHistory.Enabled = checkBoxUseGlobalHistory.Checked;
        }

        internal class LanguageItem
        {
            public string DisplayName { get; set; }

            public int LCID { get; set; }

            public override string ToString()
            {
                return this.DisplayName;
            }
        }
    }
}
